using ApiExercise.Entity;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Linq;

namespace ApiExercise.Controllers
{
    [ApiController]
    [Route("[controller]")]
    public class UsersController:ControllerBase   //MVC
    {
        //获取所有用户
         [HttpGet]
         public dynamic GetUsers(){
             var users=Users();
             return users;
         }

        //根据ID查找用户
         [HttpGet("{id}")]
         public dynamic GetId(int id){
             var users=Users();
             var user=users.Where(x=>x.Id==id).FirstOrDefault();
             return user;
         }
        
        //创建用户
         [HttpPost]
         public dynamic Created(dynamic model){
             return new{
                 Code=1000,
                 Data=model,
                 Msg="创建用户成功！"
             };
         }

        //根据ID修改用户
        [HttpPut("{id}")]
        public dynamic Updated(int id,dynamic model){
            return new{
                Code=1000,
                Data=model,
                Msg=string.Format("用户{0}修改成功！",id)
            };
        }

        //根据ID删除用户
        [HttpDelete("{id}")]
        public dynamic Deleted(int id){
            return new {
                Code=1000,
                Msg=string.Format("用户{0}删除成功！",id)
            };
        }

        //获取创建的表，并传入数据
        [HttpGet]
        public IEnumerable<dynamic>Users()
        {
            var users=new List<Users>{
                new Users{
                    Id=1,
                    UserName="admin",
                    PassWord="123"
                },
                new Users{
                    Id=2,
                    UserName="abc",
                    PassWord="123"
                },
                new Users{
                    Id=3,
                    UserName="adf",
                    PassWord="123"
                }
            };
            return users;
        }
    }
}